package bs.app.android;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Connector {

	public static final int READ = 1;
	public static Connection open(String url) throws IOException {
		url = url.toLowerCase();
		String protocols="socket";
//		String host="192.168.1.2";
//		String port="43";
		
		int iSpl = url.indexOf("://");
		if(iSpl==-1) return null;
		int iDot = url.lastIndexOf(':');
		
		protocols = url.substring(0, iSpl).trim();
		//System.out.println("--- protocols:=["+protocols+"]");
		
		if(protocols.equals("socket")){
			if(iDot - (iSpl+3) <= 0)
				return null;
			if(iDot+1 >= url.length())
				return null;
			String host = url.substring(iSpl+3, iDot);
			String port = url.substring(iDot+1).trim();
			//System.out.println("Connection >> "+protocols+"://"+host+":"+port);
			return new SocketConnection(host, Integer.parseInt(port));
		}else
		
		if(protocols.equals("http")){
			return new HttpConnection(url);
		}else
		
		if(protocols.equals("udp")){
			//TODO:
		}else

		if(protocols.equals("comm")){
			//TODO:
		}else

		if(protocols.equals("sms")){
			//TODO:
		}
		
		return null;
	}

	/** Create and open a Connection.*/
	public static Connection open(String name, int mode) throws IOException {
		return open(name);
	}
	/**Create and open a Connection.*/
	public static Connection open(String name, int mode, boolean timeouts) throws IOException {
		return open(name);
	}
	/**Create and open a connection input stream.*/
	public static DataInputStream openDataInputStream(String name) throws IOException {
		Connection cn = open(name);
		return cn.openDataInputStream();
	}
	/**Create and open a connection output stream.*/
	public static DataOutputStream openDataOutputStream(String name) throws IOException {
		Connection cn = open(name);
		return cn.openDataOutputStream();
	}
	/**Create and open a connection input stream.*/
	public static InputStream openInputStream(String name) throws IOException {
		Connection cn = open(name);
		return cn.openInputStream();
	}
	/**Create and open a connection output stream.*/
	public static OutputStream openOutputStream(String name) throws IOException {
		Connection cn = open(name);
		return cn.openOutputStream();
	}
}
